name: Verify Master Branch on Windows
on:
  workflow_dispatch:
  push:
    branches: [master]
  pull_request:
    paths:
      - '.github/workflows/verify_windows.yml'

permissions:
  contents: read

jobs:
  build:
    runs-on: windows-2022

    strategy:
      fail-fast: false
      matrix:
        node-version: [18.x, 20.x]

    env:
      CI: true
      NODE_OPTIONS: ${{ matrix.node-version == '20.x' && '--max-old-space-size=8192 --no-node-snapshot' || '--max-old-space-size=8192' }}
      INTEGRATION_TEST_GITHUB_TOKEN: ${{ secrets.INTEGRATION_TEST_GITHUB_TOKEN }}
      INTEGRATION_TEST_GITLAB_TOKEN: ${{ secrets.INTEGRATION_TEST_GITLAB_TOKEN }}
      INTEGRATION_TEST_BITBUCKET_TOKEN: ${{ secrets.INTEGRATION_TEST_BITBUCKET_TOKEN }}
      INTEGRATION_TEST_AZURE_TOKEN: ${{ secrets.INTEGRATION_TEST_AZURE_TOKEN }}

    steps:
      - name: Harden Runner
        uses: step-security/harden-runner@63c24ba6bd7ba022e95695ff85de572c04a18142 # v2.7.0
        with:
          egress-policy: audit

      - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

      - name: use node.js ${{ matrix.node-version }}
        uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
        with:
          node-version: ${{ matrix.node-version }}
          registry-url: https://registry.npmjs.org/ # Needed for auth

      # Windows file operation slowness means there's no point caching this
      - name: yarn install
        run: yarn install --immutable

      - name: lint
        run: yarn backstage-cli repo lint

      - name: type checking and declarations
        run: yarn tsc:full

      - name: verify type dependencies
        run: yarn lint:type-deps

      - name: test
        run: yarn backstage-cli repo test --maxWorkers=3 --workerIdleMemoryLimit=1300M
        env:
          BACKSTAGE_TEST_DISABLE_DOCKER: 1

      # credit: https://github.com/appleboy/discord-action/issues/3#issuecomment-731426861
      - name: Discord notification
        if: ${{ failure() }}
        env:
          DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
        run: |
          $MESSAGE=@"
          {\"content\": \"Windows master build failed https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}\"}
          "@
          C:\msys64\usr\bin\curl.exe -i -H "Accept: application/json" -H "Content-Type:application/json" -X POST $env:DISCORD_WEBHOOK --data $MESSAGE
